//package com.plian.system.service.hr.recruitment.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.hr.recruitment.PartTimeInfo;
//import com.plian.system.entity.hr.recruitment.RemovalManagement;
//import com.plian.system.enumeratea.PartTimeStatus;
//import com.plian.system.mapper.hr.recruitment.RemovalManagementMapper;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.hr.recruitment.IPartTimeInfoService;
//import com.plian.system.service.hr.recruitment.IRemovalManagementService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.hr.recruitment.RemovalManagementVO;
//import com.plian.system.wrapper.hr.recruitment.RemovalManagementWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// *  服务实现类
// *
// * @author BladeX
// * @since 2019-12-03
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class RemovalManagementServiceImpl extends BaseServiceImpl<RemovalManagementMapper, RemovalManagement> implements IRemovalManagementService {
//
//	private IPartTimeInfoService partTimeInfoService;
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private RemovalManagementWrapper removalManagementWrapper;
//
//    @Override
//    public boolean saveOrUpdate(RemovalManagement removalManagement) {
//
//        return super.saveOrUpdate(removalManagement);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            RemovalManagement removalManagement = getById(id);
//            if (removalManagement == null) {
//                return false;
//            } else if (removalManagement.getStatus() != null &&
//                    (removalManagement.getStatus() != FormStatusConstant.STASH && removalManagement.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//            partTimeInfoService.changeStatusById(removalManagement.getUpstreamCodeId(), PartTimeStatus.INCUMBENCY.getStatus());
//        }
//        return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean  finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public Boolean cancel(Long id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            RemovalManagement removalManagement = getById(id);
//            /**
//             * 设置审批人和审批时间
//             */
//            removalManagement.setStatus(updateStatus);
//			if(updateStatus == FormStatusConstant.FLOW || updateStatus == FormStatusConstant.FINISH){
//				PartTimeInfo partTimeInfo = partTimeInfoService.getById(removalManagement.getUpstreamCodeId());
//                partTimeInfo.setStatus(PartTimeStatus.REMOVAL_AUDIT.getStatus());
//                if (updateStatus == FormStatusConstant.FINISH){
//                    removalManagement.setApprovedUser(TokenUtil.getTokenUserId());
//                    Date now = DateUtil.now();
//                    removalManagement.setApprovedTime(now);
//                    partTimeInfo.setStatus(PartTimeStatus.REMOVAL.getStatus());
//                }
//                partTimeInfoService.saveOrUpdate(partTimeInfo);
//			}
//            updateById(removalManagement);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//	@Override
//	public boolean change(RemovalManagement removalManagement) {
//		if (StringUtil.isEmpty(removalManagement.getVersion())) {
//			removalManagement.setVersion("1");
//		} else {
//			removalManagement.setVersion(String.valueOf(Integer.valueOf(removalManagement.getVersion())+1));
//		}
//
//		removalManagement.setUpdateTime(new Date());
//		UpdateWrapper<RemovalManagement> updateWrapper = new UpdateWrapper<>();
//		return update(removalManagement,updateWrapper.eq("id",removalManagement.getId()));
//	}
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public Boolean submit(RemovalManagement removalManagement) {
//        if (removalManagement.getId()==null){
//            removalManagement.setVersion("1");
//            removalManagement.setCode(formCodeService.getCode(FormTypeConstant.HR_REMOVAL_FORM));
//            removalManagement.setFormType(String.valueOf(FormTypeConstant.HR_REMOVAL_FORM));
//        }
//        boolean result = saveOrUpdate(removalManagement);
//       // partTimeInfoService.changeStatusById(removalManagement.getUpstreamCodeId(), PartTimeStatus.REMOVAL_AUDIT.getStatus());
//
//        HashMap<String, Object> processVariable = new HashMap<>();
//        formWorkflowService.startProcessAndSimulate(String.valueOf(removalManagement.getId()),removalManagement.getFormType(),processVariable);
//
//        return result;
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        LambdaQueryWrapper<RemovalManagement> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(RemovalManagement::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//        List<RemovalManagement> recruitmentManagements = list(queryWrapper);
//        List<RemovalManagementVO> recruitmentManagementVOS = removalManagementWrapper.entityToVO(recruitmentManagements);
//        List<PageData> pageDataList = new ArrayList<>();
//        try{
//            if (CollectionUtil.isNotEmpty(recruitmentManagementVOS)){
//                for (RemovalManagementVO vo : recruitmentManagementVOS){
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getCompanyName());
//                    pd.put("APPLICANTNAME", vo.getCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getCompanyNameId());
//                    pd.put("formType", FormTypeConstant.HR_REMOVAL_FORM);
//                    pd.put("orgName", vo.getCompanyName());
//                    pageDataList.add(pd);
//                }
//            }
//
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        RemovalManagement removalManagement = getById(Long.valueOf(formId));
//        PartTimeInfo partTimeInfo = partTimeInfoService.getById(removalManagement.getUpstreamCodeId());
//        partTimeInfo.setStatus(PartTimeStatus.REMOVAL_AUDIT.getStatus());
//        partTimeInfoService.saveOrUpdate(partTimeInfo);
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//}
